import 'dart:collection';

import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hive/hive.dart';

import '../../api/api_client.dart';
import '../../models/user.dart';
import '../../models/user_company.dart';
import 'home_event.dart';
import 'home_state.dart';


class HomeBloc extends Bloc<HomeEvent, HomeState> {
  final ApiClient apiClient;

  HomeBloc({required this.apiClient}) : super(HomeInitial()) {
    on<LoadHomeinit>(_onCheckHome);
    on<LoadHomeData>(_onLoadHomeData);
    on<ChangeYear>(_onChangeYear);
    on<AddCompany>(_onAddCompany);
    on<AddSwiftCompany>(_onSwiftAddCompany);
  }

  Future<void> _onSwiftAddCompany(AddSwiftCompany event, Emitter<HomeState> emit) async {
    final user = Hive.box<User>('user_box').get('currentUser');
    try {
      user?.appCompanyDto = event.companyDto;
      await Hive.box<User>('user_box').put('currentUser', user!);
    } catch (e) {
    }
  }
  Future<void> _onAddCompany(AddCompany event, Emitter<HomeState> emit) async {
    emit(HomeLoading());
    final user = Hive.box<User>('user_box').get('currentUser');
    try {
      var mapData = LinkedHashMap<String, dynamic>();
      mapData.addAll(event.formData);
      mapData['userId'] = user?.id;

      final response = await apiClient.companySave(mapData);
      if(response.isSuccessful) {
        user?.appCompanyDto = response?.data as AppCompanyDto;
        await Hive.box<User>('user_box').put('currentUser', user!);
        emit(HomeAddSuccess(response?.message?? '添加成功'));
      }else {
        emit(HomeLoadFailure(response.message ?? '添加失败'));
      }
    } catch (e) {
      emit(HomeLoadFailure(e.toString()));
    }
  }


  Future<void> _onCheckHome(LoadHomeinit event, Emitter<HomeState> emit) async {
    final user = Hive.box<User>('user_box').get('currentUser');
    if (user?.appCompanyDto == null) {
      emit(HomeNoCompany('请先绑定企业信息'));
    }else{
      emit(HomeHaveCompany(user?.appCompanyDto?.taxCode != null?
          '企业信息已绑定，税号：${user?.appCompanyDto?.taxCode}' : '请先绑定企业信息'));
    }
  }

  Future<void> _onLoadHomeData(LoadHomeData event, Emitter<HomeState> emit) async {
    print("onLoadHomeData  ---------  year: ${event.year}");
    emit(HomeLoading());
    try {
      final user = Hive.box<User>('user_box').get('currentUser');
      final summary = await apiClient.getFinancialSummary(user?.appCompanyDto?.taxCode?? '', event.year.toString());
      print('summary.data------->: ${summary.data}');
      emit(HomeLoadSuccess(summary.data!));
    } catch (e) {
      emit(HomeLoadFailure(e.toString()));
    }
  }

  Future<void> _onChangeYear(ChangeYear event, Emitter<HomeState> emit) async {
    // emit(HomeChangeYear(event.year));
    // emit(HomeLoading());
    // try {
    //   final user = Hive.box<User>('user_box').get('currentUser');
    //   final summary = await apiClient.getFinancialSummary(user?.appCompanyDto?.taxCode?? '', '2025');
    //   emit(HomeLoadSuccess(summary.data as Map<String, dynamic>));
    // } catch (e) {
    //   emit(HomeLoadFailure(e.toString()));
    // }
  }


}